/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.matrices.lista;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Lista;
import clases.Usuario;
import clases.Valor;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author andreth
 */
public class AgregarValor extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String name = "Agregar Lista";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Lista l = (Lista) form;
        ArrayList<Valor> valor = new ArrayList<Valor>();
        ArrayList<Valor> valores = l.getValores();

        int numMax = l.getNumeroValores();
        System.out.println("NumeroValores" + numMax);
        for (int i = 0; i < numMax; i++) {
            valor.add(valores.get(i));
            System.out.println("Nos imprimimos " + i);
        }
        l.setValores(valor);
        for (Valor v : l.getValores()) {
            System.out.println("Valores recibidos en agregar valor" + v.getValor() + l.getIdLista());
        }
        if (DataBaseManager.getInstance().agregarValores(l)) {
            System.out.println("Ejecutando agregacion de valores exitosa");
            l.setMensajeOK("Valores agregados exitosamente");
            ArrayList<Lista> listas = DataBaseManager.getInstance().consultarListas();
            request.setAttribute("listas", listas);
            return mapping.findForward(SUCCESS);
        } else {
            l.setMensaje("Valores no agregados");
            System.out.println("Ejecutando agregacion de valores fallida");
            return mapping.findForward(FAILURE);
        }
    }
}
